package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DataStorageClasses.TimeSheet;
import Database.DatabaseConnector;

@WebServlet("/AddTimeSheetServlet")
public class AddTimeSheetServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		DatabaseConnector db = new DatabaseConnector();
		boolean failed = false;
		String message = "";
		long jobID = -1, empID=-1;
		double hours=-1;
		boolean payed=false;
		Date date=null;
		
		try {
			jobID = Long.parseLong(request.getParameter("jobID"));
			empID = Long.parseLong(request.getParameter("empID"));
			Calendar calendar = Calendar.getInstance();
			String[] dateString = request.getParameter("date").split("-");
			calendar.set(Integer.parseInt(dateString[0]), Integer.parseInt(dateString[1])-1, Integer.parseInt(dateString[2]));
			date = new Date(calendar.getTimeInMillis());
			
			try {
				payed = Boolean.parseBoolean(request.getParameter("payed"));
				hours = Double.parseDouble(request.getParameter("hours"));
				
			} catch (NumberFormatException e){
				failed = true;
				message = "Hours must be a number";
			} catch (NullPointerException e) {
			}
			
		} catch (NullPointerException e) {
			failed = true;
			message = "Job ID and Employee ID must be numbers and Date must be a date of the form yyyy-mm-dd";
		} catch (NumberFormatException e) {
			failed = true;
			message = "Job ID and Employee ID must be numbers and Date must be a date of the form yyyy-mm-dd";
		} catch (ArrayIndexOutOfBoundsException e) {
			failed = true;
			message = "Job ID and Employee ID must be numbers and Date must be a date of the form yyyy-mm-dd";
		}
		
		if (!failed) {
			TimeSheet sheet = db.addTimeSheet(jobID, empID, date, hours, payed);
			
			if (sheet == null) {
				failed = true;
				message = "Job ID and Employee ID must refer to valid jobs and employees";
			}
			
			else {
				try {
					response.sendRedirect("/GoGoGadget/TimeSheet.jsp?jobID=" + sheet.getJobID() + "&empID=" + sheet.getEmployeeID()
												+ "&date=" + sheet.getDate().toString());
					response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
					return;
				} catch (IOException e) {
					failed = true;
					message = "Could not redirect to Time Sheet page";
				}
				
			}
		}
		request.setAttribute("failed", failed);
		request.setAttribute("message", message);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/CreateTimeSheet.jsp");
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}
}
